<template>
  <view class="add-note-container">
    <!-- 顶部栏 -->
    <view class="header">
      <view class="header-left">
        <view class="cancel-btn" @tap="cancelAdd">
          <text class="cancel-text">取消</text>
        </view>
        <text class="header-title">新建笔记</text>
      </view>
      <view class="save-btn" @tap="saveNewNote" :class="{ 'disabled': !canSave }">
        <text class="save-text">完成</text>
      </view>
    </view>
    
    <!-- 笔记内容编辑区域 -->
    <view class="note-content-area">
      <input type="text" placeholder="笔记标题" v-model="newNote.title" class="note-title-input" focus="true"/>
      <textarea placeholder="开始输入笔记内容..." v-model="newNote.content" class="note-content-input" auto-height="true" show-confirm-bar="true"/>
      
      <!-- 分类选择 -->
      <view class="category-section">
        <text class="section-title">选择分类</text>
        <view class="category-list">
          <view v-for="category in categories" :key="category.id" class="category-item" :class="{ 'selected': newNote.category === category.id }" @tap="selectCategory(category.id)">
            <text class="category-icon">{{ category.icon }}</text>
            <text class="category-name">{{ category.name }}</text>
          </view>
        </view>
      </view>
      
      <!-- 图片上传 -->
      <view class="image-upload-section">
        <text class="section-title">添加图片</text>
        <view class="image-upload-area">
          <view v-for="(image, index) in noteImages" :key="index" class="image-preview">
            <image :src="image" mode="aspectFill" class="preview-image"/>
            <view class="delete-image-btn" @tap="deleteImage(index)">
              <text class="delete-icon">×</text>
            </view>
          </view>
          <view v-if="noteImages.length < 9" class="upload-btn" @tap="chooseImage">
            <text class="upload-icon">+</text>
            <text class="upload-text">添加图片</text>
          </view>
        </view>
        <text class="image-count" v-if="noteImages.length > 0">{{ noteImages.length }}/9</text>
      </view>
      
      <!-- 提醒设置 -->
      <view class="reminder-section" @tap="toggleReminder">
        <text class="section-title">设置提醒</text>
        <view class="reminder-status">
          <text class="reminder-text">{{ reminderEnabled ? '已设置提醒' : '未设置提醒' }}</text>
          <text class="arrow-icon">→</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      newNote: {
        id: '',
        title: '',
        content: '',
        date: '',
        category: 'life'
      },
      noteImages: [],
      reminderEnabled: false,
      reminderTime: '',
      categories: [
        { id: 'work', name: '工作', icon: '💼' },
        { id: 'study', name: '学习', icon: '📚' },
        { id: 'life', name: '生活', icon: '🏠' },
        { id: 'idea', name: '创意', icon: '💡' }
      ]
    };
  },
  onLoad() {
    // 生成新笔记ID
    this.newNote.id = Date.now().toString();
  },
  computed: {
    // 判断是否可以保存笔记
    canSave() {
      return this.newNote.title.trim().length > 0;
    }
  },
  methods: {
    // 取消添加笔记
    cancelAdd() {
      // 检查是否有未保存的内容
      if (this.hasUnsavedContent()) {
        uni.showModal({
          title: '提示',
          content: '您有未保存的内容，确定要离开吗？',
          success: (res) => {
            if (res.confirm) {
              uni.navigateBack();
            }
          }
        });
      } else {
        uni.navigateBack();
      }
    },
    
    // 检查是否有未保存的内容
    hasUnsavedContent() {
      return this.newNote.title.trim().length > 0 || 
             this.newNote.content.trim().length > 0 || 
             this.noteImages.length > 0;
    },
    
    // 保存新笔记
    saveNewNote() {
      if (!this.canSave) {
        return;
      }
      
      // 更新笔记日期
      this.newNote.date = this.getCurrentTime();
      
      // 模拟保存操作
      uni.showLoading({
        title: '保存中...'
      });
      
      setTimeout(() => {
        // 这里应该调用API保存笔记数据
        console.log('保存新笔记:', this.newNote);
        console.log('保存图片:', this.noteImages);
        
        // 保存成功后返回笔记列表页
        uni.hideLoading();
        uni.showToast({
          title: '创建成功',
          icon: 'success'
        });
        
        setTimeout(() => {
          uni.navigateBack();
        }, 1000);
      }, 800);
    },
    
    // 选择分类
    selectCategory(categoryId) {
      this.newNote.category = categoryId;
    },
    
    // 选择图片
    chooseImage() {
      const maxCount = 9 - this.noteImages.length;
      
      uni.chooseImage({
        count: maxCount,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          // 将选择的图片添加到列表中
          this.noteImages = [...this.noteImages, ...res.tempFilePaths];
        }
      });
    },
    
    // 删除图片
    deleteImage(index) {
      this.noteImages.splice(index, 1);
    },
    
    // 切换提醒设置
    toggleReminder() {
      if (this.reminderEnabled) {
        // 已有提醒，取消提醒
        uni.showModal({
          title: '取消提醒',
          content: '确定要取消提醒吗？',
          success: (res) => {
            if (res.confirm) {
              this.reminderEnabled = false;
              this.reminderTime = '';
            }
          }
        });
      } else {
        // 没有提醒，设置提醒
        this.setReminder();
      }
    },
    
    // 设置提醒时间
    setReminder() {
      // 模拟设置提醒功能
      // 实际项目中应该调用微信的时间选择器API
      this.reminderEnabled = true;
      this.reminderTime = '明天 10:00';
      
      uni.showToast({
        title: '提醒设置成功',
        icon: 'success'
      });
    },
    
    // 获取当前时间
    getCurrentTime() {
      const now = new Date();
      const hour = now.getHours().toString().padStart(2, '0');
      const minute = now.getMinutes().toString().padStart(2, '0');
      
      // 判断是否是今天
      const today = new Date();
      today.setHours(0, 0, 0, 0);
      
      if (now >= today) {
        return `今天 ${hour}:${minute}`;
      } else {
        const month = (now.getMonth() + 1).toString().padStart(2, '0');
        const date = now.getDate().toString().padStart(2, '0');
        return `${month}-${date} ${hour}:${minute}`;
      }
    }
  }
};
</script>

<style scoped>
.add-note-container {
  height: 100vh;
  background-color: #f8f9fc;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* 顶部栏 */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 40rpx;
  background-color: #fff;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.header-left {
  display: flex;
  align-items: center;
}

.cancel-btn {
  margin-right: 20rpx;
}

.cancel-text {
  font-size: 32rpx;
  color: #666;
}

.header-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
}

.save-btn {
  padding: 10rpx 20rpx;
  border-radius: 40rpx;
}

.save-btn.disabled {
  opacity: 0.5;
}

.save-text {
  font-size: 32rpx;
  color: #667eea;
  font-weight: 500;
}

/* 笔记内容区域 */
.note-content-area {
  padding: 30rpx;
  background-color: #fff;
}

.note-title-input {
  font-size: 40rpx;
  font-weight: 600;
  color: #333;
  padding: 10rpx 0;
  margin-bottom: 20rpx;
  border: none;
  outline: none;
  width: 100%;
}

.note-content-input {
  font-size: 32rpx;
  color: #666;
  line-height: 1.8;
  padding: 0;
  border: none;
  outline: none;
  width: 100%;
  min-height: 300rpx;
}

/* 分类选择 */
.category-section {
  margin-top: 40rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.category-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 160rpx;
  height: 120rpx;
  background-color: #f2f2f2;
  border-radius: 15rpx;
  transition: all 0.3s ease;
}

.category-item.selected {
  background-color: #e3f2fd;
  border: 2rpx solid #667eea;
}

.category-icon {
  font-size: 40rpx;
  margin-bottom: 10rpx;
}

.category-name {
  font-size: 24rpx;
  color: #666;
}

/* 图片上传 */
.image-upload-section {
  margin-top: 40rpx;
}

.image-upload-area {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  margin-bottom: 10rpx;
}

.image-preview {
  width: 160rpx;
  height: 160rpx;
  border-radius: 15rpx;
  overflow: hidden;
  position: relative;
}

.preview-image {
  width: 100%;
  height: 100%;
}

.delete-image-btn {
  position: absolute;
  top: 0;
  right: 0;
  width: 50rpx;
  height: 50rpx;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom-left-radius: 15rpx;
}

.delete-icon {
  font-size: 36rpx;
  color: #fff;
  line-height: 1;
}

.upload-btn {
  width: 160rpx;
  height: 160rpx;
  background-color: #f2f2f2;
  border-radius: 15rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.upload-icon {
  font-size: 48rpx;
  color: #999;
  margin-bottom: 10rpx;
}

.upload-text {
  font-size: 24rpx;
  color: #999;
}

.image-count {
  font-size: 24rpx;
  color: #999;
  text-align: right;
  display: block;
}

/* 提醒设置 */
.reminder-section {
  margin-top: 40rpx;
  padding: 20rpx 0;
  border-top: 1rpx solid #eee;
}

.reminder-status {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20rpx;
}

.reminder-text {
  font-size: 28rpx;
  color: #666;
}

.arrow-icon {
  font-size: 28rpx;
  color: #999;
}
</style>